package com.zyit.web;

import com.zyit.DataObject.T_user;
import com.zyit.service.Impl.UserServiceImpl;
import com.zyit.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        System.out.println(account);
        System.out.println(password);
        // 在这里进行账号和密码的验证逻辑，可以查询数据库或使用其他认证方式。
        T_user user = userService.findUserNameAndPassword(account, password);

        if (user != null ) {
            // 获取当前会话，如果没有会话则创建一个
            HttpSession session = request.getSession();
            System.out.println(user);
            // 将用户信息存储在会话中
            session.setAttribute("userI", user);
            Integer type = user.getType();
            session.setAttribute("typeI",type);
            response.getWriter().write("{\"status\": 1, \"message\": \"登录成功\"}");
        } else {
            // 登录失败，返回错误的JSON响应
            response.getWriter().write("{\"status\": 0, \"message\": \"登录失败\"}");
        }
    }

}
